package com.oa.hyperion.api.v1;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.oa.hyperion.entity.*;
import com.oa.hyperion.service.*;
import com.oa.hyperion.vo.InformationVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value="通知信息controller",tags={"通知信息操作接口"})
@RestController
@RequestMapping("/v1/information")
public class InformationController {
    @Autowired
    private InformationService informationService;
    @Autowired
    private DepartmentMessageMergeService departmentMessageMergeService;
    @Autowired
    private EmployeeMessageMergeService employeeMessageMergeService;
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private PostService postService;
    /**
     * 获取未读所有通知信息
     */
    @ApiOperation(value="获取未读所有通知信息")
    @GetMapping("getAll/{id}")
    public List<Information> getAll(@ApiParam(name = "id",value = "个人的ID") @PathVariable Integer id){
        Map<String, Object> map1 =new HashMap<>();
        map1.put("employee_id",id);
        Employee byId1 = employeeService.getById(id);
        Post byId2 = postService.getById(byId1.getPost());
        Map<String, Object> map2 =new HashMap<>();
        map2.put("department_id",byId2.getDepartment());
        List<EmployeeMessageMerge> employeeMessageMerges = employeeMessageMergeService.listByMap(map1);
        List<DepartmentMessageMerge> departmentMessageMerges = departmentMessageMergeService.listByMap(map2);
        Map<String, Object> map =new HashMap<>();
        List<Information> list = new ArrayList<>();
        employeeMessageMerges.forEach(e->{
            map.put("id", e.getInformationId());
            map.put("rea","m");
            Information byId = informationService.getById(map);
            list.add(byId);
        });
        departmentMessageMerges.forEach(e->{
            map.put("id", e.getInformationId());
            map.put("rea","m");
            Information byId = informationService.getById(map);
            list.add(byId);
        });
        return list;
    }
    /**
     * 修改已读通知信息状态
     */
    @ApiOperation(value="修改已读通知信息状态")
    @PutMapping("updateByStatus")
    public boolean updateAllStatus(Integer id){
        DepartmentMessageMerge byId = departmentMessageMergeService.getById(id);
        Information information = informationService.getById(byId.getInformationId());
        information.setRea("y");
        boolean flag=informationService.updateById(information);
        return flag;
    }
    /**
     * 通过ID获取指定通知信息
     */
    @ApiOperation(value="通过ID获取指定通知信息")
    @GetMapping("getById/{id}")
    public Information getById(@ApiParam(name = "id",value = "个人的ID") @PathVariable Integer id){
        Information list=  informationService.getById(id);
        return list;
    }
    /**
     * 添加通知
     * @RequestBody 允许参数是JSON格式
     */
    @ApiOperation(value="创建通知通知信息或者发布")
    @PostMapping("add")
    public boolean addInformation(@RequestBody InformationVo informationVo){
        boolean flag=informationService.save(informationVo);
        return flag;
    }
    /**
     * 添加通知
     * @RequestBody 允许参数是JSON格式
     */
    @ApiOperation(value="发布通知公告信息")
    @GetMapping("adds/{id}")
    public boolean addInformations(@ApiParam(name = "id",value = "个人的ID") @PathVariable Integer id){
        Information byId = informationService.getById(id);
        byId.setStatus("f");
        return informationService.updateById(byId);
    }
    /**
     * 逻辑删除:通知
     *  将传入得 通知信息id 作为依据进行删除
     *  删除成功：返回true
     *      否则返回 false
     */
    @ApiOperation(value="通过通知信息id 作为依据进行删除")
    @PostMapping("delete")
    public boolean deleteDepartment(Integer id){
        departmentMessageMergeService.removeById(id);
        return informationService.removeById(id);
    }
}
